package cn.waka.sys.admin.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Component;

import cn.waka.sys.admin.object.App;
import cn.waka.sys.admin.object.Page;

@Component
public class AppDao extends BaseDao {

    public List<App> findList(Page p, Map<String, String> param) {

        int page = p.getPage();
        int start = (page - 1) * p.getSize();

        String sql = "select id,name,app_key,app_secret,status,user_id,expired_time,type from b_app order by id desc";
        Object[] dbparams = null;
        if (param != null) {
            Set<String> pks = param.keySet();
            dbparams = new Object[2 + param.size()];
            int i = 0;

            if (param.size() > 0) {
                for (String pa : pks) {
                    sql += " and " + pa + " like ?";
                    dbparams[i] = param.get(pa);
                    i++;
                }

                dbparams[param.size()] = start;
                dbparams[param.size() + 1] = p.getSize();
            } else {
                dbparams[0] = start;
                dbparams[1] = p.getSize();
            }

        } else {
            dbparams = new Object[2];
            dbparams[0] = start;
            dbparams[1] = p.getSize();
        }
        sql += " limit ?,?";
        List<App> list = this.query(sql, new RowMapper<App>() {
            @Override
            public App mapRow(ResultSet rs, int rowNum) throws SQLException {
                App a = new App();
                a.setId(rs.getInt(1));
                a.setName(rs.getString(2));
                a.setAppKey(rs.getString(3));
                a.setAppSecret(rs.getString(4));
                a.setStatus(rs.getBoolean(5));
                a.setCreateUserId(rs.getInt(6));
                a.setExpiredTime(rs.getTimestamp(7));
                a.setType(rs.getInt(8));
                return a;
            }

        }, dbparams);
        return list;
    }

    public int findCount(Page p, Map<String, String> param) {
        String sql = "select count(1) from b_app";
        Object[] dbparams = null;
        if (param != null) {
            Set<String> pks = param.keySet();
            dbparams = new Object[param.size()];
            int i = 0;
            if (param.size() > 0) {
                for (String pa : pks) {
                    sql += " and " + pa + " like ?";
                    dbparams[i] = param.get(pa);
                    i++;
                }
            }
        }
        int c = 0;
        if (param != null && param.size() > 0) {
            c = this.queryForObject(sql, Integer.class, dbparams);
        } else {
            c = this.queryForObject(sql, Integer.class);
        }
        return c;
    }

    public App findById(int id) {
        String sql = "select id,name,app_key,app_secret,status,user_id,expired_time,type from b_app where id=?";
        List<App> list = this.query(sql, new RowMapper<App>() {
            @Override
            public App mapRow(ResultSet rs, int rowNum) throws SQLException {
                App a = new App();
                a.setId(rs.getInt(1));
                a.setName(rs.getString(2));
                a.setAppKey(rs.getString(3));
                a.setAppSecret(rs.getString(4));
                a.setStatus(rs.getBoolean(5));
                a.setCreateUserId(rs.getInt(6));
                a.setExpiredTime(rs.getTimestamp(7));
                a.setType(rs.getInt(8));
                return a;
            }

        }, id);
        return list.size() > 0 ? list.get(0) : null;
    }

    public int insert(final App a) {
        KeyHolder holder = new GeneratedKeyHolder();
        this.update(new PreparedStatementCreator() {

            @Override
            public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                String sql = "insert into b_app(name,app_key,app_secret,`status`,user_id,expired_time,type)values(?,?,?,?,?,?,?)";
                PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                ps.setString(1, a.getName());
                ps.setString(2, a.getAppKey());
                ps.setString(3, a.getAppSecret());
                ps.setBoolean(4, a.isStatus());
                ps.setInt(5, a.getCreateUserId());
                ps.setTimestamp(6, a.getExpiredTime());
                ps.setInt(7, a.getType());
                return ps;
            }
        }, holder);
        int id = holder.getKey().intValue();
        return id;
    }

    public int update(App a) {
        int r = this.update("update b_app set name=?,user_id=?,expired_time=?,type=? where id=?", a.getName(),

                a.getCreateUserId(), a.getExpiredTime(), a.getType(), a.getId());
        return r;
    }

    public int updateStatus(int id, int status) {
        int r = this.update("update b_app set status=? where id=?", status, id);
        return r;
    }

    public int del(int uid) {
        int r = this.update("update s_user set is_del=1 where uid=?", uid);
        return r;
    }
}
